package com.oracle.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.compents.PageResult;
import com.oracle.mapper.PhGoodsMapper;
import com.oracle.pojo.PhGoods;
import com.oracle.pojo.PhGoodsExample;
import com.oracle.pojo.PhGoodsExample.Criteria;
import com.oracle.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	private PhGoodsMapper goodsMapper;

	/**
	 * 查询全部
	 */
	@Override
	public List<PhGoods> findAll() {
		return goodsMapper.selecPhyExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<PhGoods> page=   (Page<PhGoods>) goodsMapper.selecPhyExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(PhGoods goods) {
		goodsMapper.insert(goods);
	}


	/**
	 * 修改
	 */
	@Override
	public void update(PhGoods goods){
		goodsMapper.updateByPrimaryKey(goods);
	}

	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public PhGoods findOne(Long id){
		return goodsMapper.selecPhyPrimaryKey(id);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			goodsMapper.deleteByPrimaryKey(id);
		}
	}


		@Override
	public PageResult findPage(PhGoods goods, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);

		PhGoodsExample example=new PhGoodsExample();
		Criteria criteria = example.createCriteria();

		if(goods!=null){
						if(goods.getGoodsname()!=null && goods.getGoodsname().length()>0){
				criteria.andGoodsnameLike("%"+goods.getGoodsname()+"%");
			}			if(goods.getGoodspic()!=null && goods.getGoodspic().length()>0){
				criteria.andGoodspicLike("%"+goods.getGoodspic()+"%");
			}			if(goods.getGoodspiclist()!=null && goods.getGoodspiclist().length()>0){
				criteria.andGoodspiclistLike("%"+goods.getGoodspiclist()+"%");
			}			if(goods.getIntroduce()!=null && goods.getIntroduce().length()>0){
				criteria.andIntroduceLike("%"+goods.getIntroduce()+"%");
			}
		}

		Page<PhGoods> page= (Page<PhGoods>)goodsMapper.selecPhyExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}

}
